package com.hq.mes.bussiness;

import java.util.Set;

import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;

import com.hq.mes.bussiness.inrecord.domain.Inrecord;
import com.hq.mes.bussiness.inrecorddetail.domain.InrecordDetail;
import com.hq.mes.bussiness.inrecorddetail.service.mapstruct.InrecordDetailMapper;
import com.hq.mes.bussiness.product.domain.Product;
import com.hq.mes.bussiness.product.service.mapstruct.ProductMapper;

import me.zhengjie.utils.StringUtils;

@Mapper(uses = { InrecordDetailMapper.class,
		ProductMapper.class }, componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public abstract class BussinessBaseMapper {

//	解决因避免json循环序列化而断开的 Entity关联关系
	@AfterMapping
	protected void postProcess(@MappingTarget Inrecord inrecord) {
		System.out.println("#######      检查并处理 inrecordDetails  " + inrecord.getName());
		Set<InrecordDetail> details = inrecord.getDetails();
		if (details != null && details.size() > 0) {
			int index = 0;
			int count = details.size();

			for (InrecordDetail detail : details) {
				index++;
				if (detail != null) {
					if (StringUtils.isBlank(detail.getName())) {
						String postStr = "_" + index + "-" + count;
						detail.setName(inrecord.getName() + postStr);
					}
					if (StringUtils.isBlank(detail.getCode())) {
						String postStr = "_" + index + "-" + count + "-" + System.currentTimeMillis();
						detail.setCode(inrecord.getCode() + postStr);
					}
//					if (detail.getInrecord() == null || detail.getInrecord().getId() == null) {
//						detail.setInrecord(inrecord);
//					}
				}
			}
		}

	}

	@AfterMapping
	protected void postProcess(@MappingTarget Product product) {

	}
	@AfterMapping
	protected void postInrecordDetail(@MappingTarget InrecordDetail inrecordDetail) {

	}
}